package david.java.tools;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: ZhaoDawei
 * @Date: Create in 2:21 下午 2022/9/15
 */
public class V2HUtil extends BaseUtil {


    public static void main(String[] args) throws IOException {
        String input = "src/tmp/src_v2h";
        String output1 = "target-tmp/tmp-v2h";
        String output2 = "target-tmp/tmp-h2v";
        mkdirIfNotExists(output1);
        mkdirIfNotExists(output2);
        v2h(input, output1, false);
        v2h(input, output2, true);

    }


    public static void v2h(String input, String output, boolean is2v) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader(input));
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        List<String> result = new ArrayList<>();

        String line;
        while ((line = br.readLine()) != null) {
            if (StringUtils.isNotBlank(line) && !line.startsWith("--")) {
                String[] split = line.split(",");
                for (String str : split) {
                    if (StringUtils.isNotBlank(str)) {
                        result.add(str);
                    }
                }
            }
        }


        bw.write(result.get(0));
        for (int i = 1; i < result.size(); i++) {
            if (is2v) {
                bw.newLine();
            }

            bw.write(",");
            bw.write(result.get(i));
        }


        bw.flush();
        br.close();
        bw.close();

    }

}
